package com.app.taobao;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.util.Properties;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.Item;
import com.taobao.api.request.*;
import com.taobao.api.response.*;
import com.utils.Constants;
import com.utils.UtilsLog;
import com.utils.UtilsProperties;

public class RequestTaoBao {
	private String sessionKey = "";
	private final static String format = "xml";
	private TaobaoClient client;
	private String login_user = "nick";
	public RequestTaoBao() {
		try {
			Properties prop = UtilsProperties.loadPropertiesByFile(Constants.APP_CONFIGFILE_PATH + "taobao_app.properties");
			String url = (String) prop.get("url");
			String appkey = (String) prop.get("client_id");
			String secret = (String) prop.get("client_secret");
			Properties prop2 = UtilsProperties.loadPropertiesByFile(Constants.APP_CONFIGFILE_PATH + "session");
			sessionKey = ((String) prop2.get(login_user)).split(",")[1];
			client = new DefaultTaobaoClient(url, appkey, secret, format);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	public void itemUpdateDescByOuterId(String outer_id, String desc,
			boolean type) throws ParseException, ApiException {
		/** 1，根据外部id获得商品num_iid,desc */
		ItemsCustomGetResponse itemsCustomGetRequest = this
				.itemsCustomGetRequest(outer_id);
		if(itemsCustomGetRequest.getItems() == null){
			UtilsLog.LOG.error(outer_id + " is not exsit");
		}else{
			Item item = itemsCustomGetRequest.getItems().get(0);
			/** 判断是否为追加模式 */
			System.out.println(desc);
			if (type) {
				desc = desc + item.getDesc();
			}
			/** 2，根据num_iid更新商品描述 */
			ItemUpdateResponse resp = this.itemUpdateRequest(
					item.getNumIid(), desc);
			
			if (resp.getErrorCode() == null) {
				UtilsLog.LOG.info("操作成功！" + outer_id + resp.getBody());
				UtilsLog.LOG.info(desc);
			} else {
				UtilsLog.LOG.info("操作失败！" + outer_id + resp.getBody());
			}
		}
	}

	/**
	 * 根据外部id获得商品num_iid,desc
	 * 
	 * @throws ApiException
	 */
	public ItemsCustomGetResponse itemsCustomGetRequest(String outer_id)
			throws ApiException {
		ItemsCustomGetRequest req = new ItemsCustomGetRequest();
		req.setOuterId(outer_id);
		req.setFields("num_iid,desc");
		ItemsCustomGetResponse response = client.execute(req, sessionKey);

		return response;
	}

	/**
	 * 根据num_iid更新商品描述（追加模式）
	 * 
	 * @throws ApiException
	 */
	public ItemUpdateResponse itemUpdateRequest(Long num_iid, String desc)
			throws ApiException {
		ItemUpdateRequest req = new ItemUpdateRequest();
		req.setNumIid(num_iid);
		req.setDesc(desc);
		ItemUpdateResponse response = client.execute(req, sessionKey);

		return response;
	}

	/**
	 * 增加一个商品
	 * @throws ApiException 
	 */
	public ItemAddResponse itemAddRequest(ItemAddRequest req1) throws ApiException {
		ItemAddResponse response = client.execute(req1 , sessionKey);
		if(response.getErrorCode() != null || response.getMsg() != null){
			UtilsLog.LOG.error(req1.getOuterId() + ", request taobao error: " + "<error_response><error_code>"+response.getErrorCode()+"</error_code><msg>"+response.getMsg()+"</msg><sub_code>"+response.getSubCode()+"</sub_code><sub_msg>"+response.getSubMsg()+"</sub_msg></error_response>");
		}else{
			UtilsLog.LOG.info(req1.getOuterId() + ", request taobao sucess: " + response.getBody());
		}

		return response;
	}
	
	public com.taobao.api.response.SkusQuantityUpdateResponse SkusQuantityUpdateRequest(com.taobao.api.request.SkusQuantityUpdateRequest req) throws ApiException{
//		com.taobao.api.request.SkusQuantityUpdateRequest req=new com.taobao.api.request.SkusQuantityUpdateRequest();
//		req.setNumIid(19869560122L);
//		req.setType(1L);
//		req.setOuteridQuantities("HCHD3K009_009F_165/84A(46):3");
		com.taobao.api.response.SkusQuantityUpdateResponse response = client.execute(req , sessionKey);
		
		return response;
	}
	public com.taobao.api.response.SkusQuantityUpdateResponse SkusQuantityUpdateRequestByOuterID(String outerId, String outeridQuantities) throws ApiException{
		ItemsCustomGetResponse itemsCustomGetRequest = this.itemsCustomGetRequest(outerId);
		Long numIid = null ;
		if (itemsCustomGetRequest.getItems() == null) {
			UtilsLog.LOG.error(outerId + " is not exsit");
			return null;
		} else {
			Item item = itemsCustomGetRequest.getItems().get(0);
			numIid = item.getNumIid();
		}
		
		com.taobao.api.request.SkusQuantityUpdateRequest req=new com.taobao.api.request.SkusQuantityUpdateRequest();
		req.setNumIid(numIid);
		req.setType(1L);
		req.setOuteridQuantities(outeridQuantities);
		
		SkusQuantityUpdateResponse resp = this.SkusQuantityUpdateRequest(req);
		
		UtilsLog.LOG.info(outerId + ": " + resp.getBody());
		
		return resp;
	}
	
	public UserSellerGetResponse userSellerGetRequest() throws ApiException{
		UserSellerGetRequest req=new UserSellerGetRequest();
		req.setFields("nick");
		UserSellerGetResponse response = client.execute(req , sessionKey);
		
		return response;
	}
	
	public ItemcatsGetResponse itemcatsGetRequest(Long parentCid) throws ApiException{
		ItemcatsGetRequest req=new ItemcatsGetRequest();
		req.setFields("cid,parent_cid,name,is_parent");
		req.setParentCid(parentCid);
		ItemcatsGetResponse response = client.execute(req);
		return response;
	}
	
	public ItempropsGetResponse itempropsGetRequest(Long cid) throws ApiException{
		ItempropsGetRequest req=new ItempropsGetRequest();
		req.setFields("pid,name,must,multi,prop_values");
		req.setCid(cid);
		ItempropsGetResponse response = client.execute(req);
		return response;
	}
}
